package com.hcrazy.manchester.app.jdbc;

import java.util.ArrayList;
import java.util.List;

import com.hcrazy.manchester.app.pojo.RadioProgram;
import com.hcrazy.manchester.app.pojo.PageModel;

public class RadioProgramDAO extends BaseDAO<RadioProgram>{

	public long addRadioProgram(RadioProgram program) throws Exception{
		StringBuilder sb = new StringBuilder();
		sb.append("insert into him_radio_program(");
		sb.append("channelId,title,programLogo,programDesc,programFrom,createDate,createUser,fromType,audioAddress,programDetails");
		sb.append(") values (");
		sb.append(":channelId,:title,:programLogo,:programDesc,:programFrom,:createDate,:createUser,:fromType,:audioAddress,:programDetails");
		sb.append(")");
		
		update(sb.toString(), program);
		
		return getIncreamentId();
	}
	
	public void updateRadioProgram(RadioProgram program) throws Exception{
		StringBuilder sb = new StringBuilder();
		sb.append("update him_radio_program set ");
		sb.append(" channelId=:channelId,title=:title,programLogo=:programLogo,programFrom=:programFrom,programDesc=:programDesc,");
		sb.append(" createDate=:createDate,createUser=:createUser,fromType=:fromType,audioAddress=:audioAddress,programDetails=:programDetails");
		sb.append(" where programId = :programId");
		
		update(sb.toString(), program);
	}
	
	public List<RadioProgram> listAllInfos() throws Exception{
		String sql = "select * from him_radio_program";
		return getList(sql, RadioProgram.class);
	}
	
	public List<RadioProgram> getAllInfosByChannelId(int channelId,boolean hasDetail) throws Exception{
		String sql = "select a.programId,a.channelId,a.title,a.programLogo,a.programDesc," +
				"a.programFrom,a.createDate,a.createUser,a.fromType,a.audioAddress,b.channelName" ;
		if(hasDetail){
			sql += ",a.programDetails";
		}
		sql += " from him_radio_program a,him_radio_channel b where a.channelId = b.channelId";
		if(channelId > 0){
			sql += " and a.channelId = ? ";
		}
		return getList(sql, RadioProgram.class,channelId);
	}
	
	public PageModel getPagedInfos(PageModel model,String search,int channelId,int fromType) throws Exception{
		StringBuilder querySQL = new StringBuilder("select * from (select a.*,b.channelName from him_radio_program a,him_radio_channel b where a.channelId = b.channelId) as c");
		StringBuilder countSQL = new StringBuilder("select count(*) from (select a.*,b.channelName from him_radio_program a,him_radio_channel b where a.channelId = b.channelId) as c ");
		StringBuilder whereSQL = new StringBuilder(" where title like ? ");
		List<Object> paramList = new ArrayList<Object>();
		if(null == search)
			search = "";
		
		paramList.add("%"+search+"%");
		if(channelId > 0){
			whereSQL.append(" and channelId = ?");
			paramList.add(channelId);
		}
		if(fromType > 0){
			whereSQL.append(" and fromType = ?");
			paramList.add(fromType);
		}
		
		whereSQL.append(" order by createDate desc");
		
//		System.out.println(querySQL+whereSQL.toString());
//		System.out.println(paramList);
		return this.getPageModel(model, querySQL, countSQL, whereSQL, paramList, RadioProgram.class);
	}
	
	public RadioProgram getById(int programId) throws Exception{
		String sql = "select * from him_radio_program where programId = ?";
		return this.getJavaBean(sql, RadioProgram.class, programId);
	}
	
	public RadioProgram getById(int programId,boolean hasDetail) throws Exception{
		if(hasDetail){
			return getById(programId);
		}
		String sql = "select a.channelId,a.programId,a.title,a.programLogo,a.programDesc," +
				"a.programFrom,a.createDate,a.createUser,a.fromType,a.audioAddress " +
				"from him_radio_program a where programId = ?";
		return this.getJavaBean(sql, RadioProgram.class, programId);
	}
	
	public void delRadioProgram(int programId) throws Exception{
		String sql = "delete from him_radio_program where programId = ?";
		this.commonUpdate(sql, programId);
	}
	

	
}
